package com.lx.utill;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.UUID;

public class JwtUtill {
    private static long time = 1000 * 60 * 60;
    private static String signature = "admin";

    public static String CreateToken() {
        JwtBuilder jwt = Jwts.builder();
        String jwtToken = jwt
                .setHeaderParam("type", "jwt")
                .setHeaderParam("alg", "HS256")
                .claim("username", "tom")
                .claim("password", "admin")
                .setSubject("admin-test")
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .setId(UUID.randomUUID().toString())
                .signWith(SignatureAlgorithm.HS256, signature)
                .compact();
        return jwtToken;
    }

    public static boolean VerifyToken(String token) {
        try {
            JwtParser jwtParser = Jwts.parser();
            Jws<Claims> claimsJws = jwtParser
                    .setSigningKey(signature)
                    .parseClaimsJws(token);
//            Claims claims = claimsJws.getBody();
//            System.out.println(claims);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
